package com.online.onlinestudent.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.online.onlinestudent.dto.AjaxResult;
import com.online.onlinestudent.entity.Course;
import com.online.onlinestudent.entity.Record;
import com.online.onlinestudent.entity.Student;
import com.online.onlinestudent.entity.Teacher;
import com.online.onlinestudent.service.CourseService;
import com.online.onlinestudent.service.RecordService;
import com.online.onlinestudent.service.StudentService;
import com.online.onlinestudent.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/teachers")
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    @Autowired
    private CourseService courseService;

    @Autowired
    private RecordService recordService;

    @Autowired
    private StudentService studentService;


    @GetMapping
    public AjaxResult getAllTeachers() {
        List<Teacher> teachers = teacherService.list();
        if (teachers == null || teachers.isEmpty()) {
            return AjaxResult.fail("未查询到结果！");
        }
        return AjaxResult.success(teachers);
    }

    @GetMapping("/{id}")
    public AjaxResult getTeacherById(@PathVariable Long id) {
        Teacher teacher = teacherService.getById(id);
        if (teacher != null) {
            return AjaxResult.success(teacher);
        } else {
            return AjaxResult.fail("不存在该教师！");
        }
    }

    @PostMapping
    public AjaxResult saveTeacher(@RequestBody Teacher teacher) {
        try {
            boolean saved = teacherService.save(teacher);
            if (saved) {
                return AjaxResult.success("插入成功！");
            } else {
                return AjaxResult.fail("插入失败！");
            }
        } catch (Exception e) {
            return AjaxResult.fail("插入失败，已存在该教师！" );
        }
    }

    @PutMapping("/{id}")
    public AjaxResult updateTeacher(@PathVariable Long id, @RequestBody Teacher teacher) {
        teacher.setId(id);
        try {
            boolean updated = teacherService.updateById(teacher);
            if (updated) {
                return AjaxResult.success("更新成功！");
            } else {
                return AjaxResult.fail("更新失败，请重新输入！");
            }
        } catch (Exception e) {
            return AjaxResult.fail("更新失败：" + e.getMessage());
        }
    }

    @DeleteMapping("/{id}")
    public AjaxResult deleteTeacherById(@PathVariable Long id) {
        try {
            boolean deleted = teacherService.removeById(id);
            if (deleted) {
                return AjaxResult.success("删除成功！");
            } else {
                return AjaxResult.fail("删除失败，不存在该教师！");
            }
        } catch (Exception e) {
            return AjaxResult.fail("删除失败：" + e.getMessage());
        }
    }

    @GetMapping("/page")
    public AjaxResult getTeachersByPage(@RequestParam int current, @RequestParam int size) {
        try {
            Page<Teacher> page = teacherService.page(new Page<>(current, size));
            if (page == null || page.getRecords().isEmpty()) {
                return AjaxResult.fail("未查询到结果！");
            }
            return AjaxResult.success(page);
        } catch (Exception e) {
            return AjaxResult.fail("查询失败：" + e.getMessage());
        }
    }
    @PostMapping("/username")
    public AjaxResult fuzzyQueryByUsername(@RequestBody Teacher teacher) {
        String username = teacher.getUsername();
        if (username == null || username.trim().isEmpty()) {
            return AjaxResult.fail("用户名不能为空！");
        }
        try {
            List<Teacher> list = teacherService.fuzzyQueryByUsername(username);
            if (list.isEmpty()) {
                return AjaxResult.fail("未查询到结果！");
            }
            return AjaxResult.success(list);
        } catch (Exception e) {
            return AjaxResult.fail("查询失败：" + e.getMessage());
        }
    }

    @PostMapping("/student")
    public AjaxResult getAllStudents(@RequestBody Teacher teacher) {


        Long id = teacher.getId();
        if (id == null ) {
            return AjaxResult.fail("教师id不能为空！");
        }
        try {
            List<Course> courses = courseService.getCoursesByTeacherId(id);
            if (courses.isEmpty()) {
                return AjaxResult.fail("未查询到教师课程！");
            }
            List<Record> records=recordService.getAllRecordByCourse(courses);
            if (records.isEmpty()) {
                return AjaxResult.fail("未查询到教师的学生！");
            }
            List<Student> students=studentService.getAllStudentByRecord(records);
            if (records.isEmpty()) {
                return AjaxResult.fail("未查询到教师的学生！");
            }

            return AjaxResult.success(students);
        } catch (Exception e) {
            return AjaxResult.fail("查询失败：" + e.getMessage());
        }
    }
}